package vip.xiaonuo.smzq.modular.kcl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.bcel.generic.IF_ACMPEQ;
import vip.xiaonuo.smzq.modular.deom.TypeProcessing;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb08XzqhMapper;
import vip.xiaonuo.smzq.modular.hisi.entity.Kshz;
import vip.xiaonuo.smzq.modular.hisi.mapper.KshzMapper;
import vip.xiaonuo.smzq.modular.kcl.entity.Kslsju;
import vip.xiaonuo.smzq.modular.kcl.entity.param.KslsParam;
import vip.xiaonuo.smzq.modular.kcl.entity.param.KszhlsVo;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.KsAutocompleteVo;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.KslsVo;
import vip.xiaonuo.smzq.modular.kcl.mapper.KslsjuMapper;
import vip.xiaonuo.smzq.modular.kcl.service.IKslsjuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 矿山历史记录表 服务实现类
 *
 * @author sqq
 * @since 2024-09-10
 */
@Service
public class KslsjuServiceImpl extends ServiceImpl<KslsjuMapper, Kslsju> implements IKslsjuService {

    @Resource
    private KslsjuMapper kslsjuMapper;
    @Resource
    private KshzMapper kshzMapper;
    @Resource
    private Cb08XzqhMapper cb08XzqhMapper;

    @Override
    public Page<Kslsju> page(Kslsju kslsju) {
        QueryWrapper<Kslsju> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }


    /**
     * 矿山历史记录表分查询
     */
    @Override
    public List<Kslsju> getList(Kslsju kslsju) {
        List<Kslsju> list =new ArrayList<>();
        try{
            LambdaQueryWrapper<Kslsju> queryWrapper = new LambdaQueryWrapper<>();
            if (kslsju.getKqbh()!=null){
                queryWrapper.eq(Kslsju::getKqbh,kslsju.getKqbh());
            }
            if (kslsju.getDjflbh()!=null){
                queryWrapper.eq(Kslsju::getDjflbh,kslsju.getDjflbh());
            }
            queryWrapper.orderByDesc(Kslsju::getJdrq);
            list= kslsjuMapper.selectList(queryWrapper);

        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 根据id查详情
     * @param kslsju
     * @return
     */
    @Override
    public List<Kslsju> getDetails(Kslsju kslsju) {
        return getList(kslsju);
    }


    /**
     * 删除矿山历史记录表
     */
    @Override
    public void removeKls(Kslsju kslsju) {
        LambdaQueryWrapper<Kslsju> queryWrapper = new LambdaQueryWrapper<>();
        //删除整个矿山历史
        if (kslsju.getKqbh()!=null&&kslsju.getDjflbh()!=null){
            queryWrapper.eq(Kslsju::getKqbh,kslsju.getKqbh()).eq(Kslsju::getDjflbh,kslsju.getDjflbh());
        }

        //多条或单条删除
        if (kslsju.getPkid()!=null){
          List<String> pkidList = TypeProcessing.strToStrList(kslsju.getPkid());
            queryWrapper.in(Kslsju::getPkid,pkidList);
        }
        kslsjuMapper.delete(queryWrapper);
    }


    public Page<KslsVo> getPageLis1t(KslsParam param) {
        Page<KslsVo> resultPage = new Page<>();
        resultPage.setSize(param.getSize());
        resultPage.setCurrent(param.getCurrent());

        Page<Kshz> page = new Page<>();
        page.setCurrent(param.getCurrent());
        page.setSize(param.getSize());

        List<String> ksxq=kslsjuMapper.getKsxxhzList();

        if (ksxq!=null&&ksxq.size()!=0) {
            // 构建IN子句
            StringBuilder inClause = new StringBuilder();
            for (int i = 0; i < ksxq.size(); i++) {
                if (i > 0) {
                    inClause.append(", ");
                }
                inClause.append("'").append(ksxq.get(i)).append("'");
            }

            QueryWrapper<Kshz> queryWrapper = new QueryWrapper<>();
            queryWrapper.inSql("(kqbh || '_' || djflbh)", inClause.toString());
           // queryWrapper.eq("nd", 2023);
            if (param.getKcdm() != null) {
                queryWrapper.eq("kcdm", param.getKcdm());
            } else {
                queryWrapper.in("kcdm", 22001, 11001, 42201, 32017, 63941, 83906);
            }
            page = kshzMapper.selectPage(page, queryWrapper);
            List<KslsVo> kslsVoList = new ArrayList<>();
            List<Kshz> kshzList = page.getRecords();
            for (Kshz kshz : kshzList) {
                KslsVo kslsVo = new KslsVo();
                kslsVo.setKsmc(kshz.getKsmc());
                kslsVo.setKcmc(kshz.getKcmc());
                kslsVo.setKsgm(kshz.getKsgm());
                kslsVo.setDjflbh(kshz.getDjflbh());
                kslsVo.setKqbh(kshz.getKqbh());
                String xzqhmc = cb08XzqhMapper.getXzqhmc(Integer.parseInt(kshz.getXzqdm()));
                kslsVo.setXzqmc(xzqhmc);
                LambdaQueryWrapper<Kslsju> queryWrapperKsls = new LambdaQueryWrapper<>();
                queryWrapperKsls.eq(Kslsju::getKqbh, kshz.getKqbh())
                        .eq(Kslsju::getDjflbh, kshz.getDjflbh());
                queryWrapperKsls.orderByDesc(Kslsju::getJdrq);
                List<Kslsju> kslsjus = kslsjuMapper.selectList(queryWrapperKsls);
                kslsVo.setKslsjus(kslsjus);
                kslsVoList.add(kslsVo);
            }
            resultPage.setRecords(kslsVoList);
            resultPage.setTotal(page.getTotal());
        }
        return resultPage;
    }
    @Override
    public Page<KslsVo> getPageList(KslsParam param) {
        Page<KslsVo> resultPage = new Page<>();
        resultPage.setSize(param.getSize());
        resultPage.setCurrent(param.getCurrent());

        Page<Kshz> page = new Page<>();
        page.setCurrent(param.getCurrent());
        page.setSize(param.getSize());

        List<String> ksxq=kslsjuMapper.getKsxxhzList();

        if (ksxq!=null&&ksxq.size()!=0) {
            // 构建IN子句
            StringBuilder inClause = new StringBuilder();
            for (int i = 0; i < ksxq.size(); i++) {
                if (i > 0) {
                    inClause.append(", ");
                }
                inClause.append("'").append(ksxq.get(i)).append("'");
            }

            QueryWrapper<Kshz> queryWrapper = new QueryWrapper<>();
            queryWrapper.inSql("(kqbh || '_' || djflbh)", inClause.toString());
            queryWrapper.eq("nd", 2023);
            if (param.getKcdm() != null) {
                queryWrapper.eq("kcdm", param.getKcdm());
            } else {
                queryWrapper.in("kcdm", 22001, 11001, 42201, 32017, 63941, 83906);
            }
            Page<KszhlsVo>  kszhlsVopage = kshzMapper.getKsPage(page, queryWrapper);
            List<KslsVo> kslsVoList = new ArrayList<>();
            List<KszhlsVo> kshzList = kszhlsVopage.getRecords();
            for (KszhlsVo kshz : kshzList) {
                KslsVo kslsVo = new KslsVo();
                kslsVo.setKsmc(kshz.getKsmc());
                kslsVo.setKcmc(kshz.getKcmc());
                kslsVo.setKsgm(kshz.getKsgm());
                kslsVo.setDjflbh(kshz.getDjflbh());
                kslsVo.setKqbh(kshz.getKqbh());
                String xzqhmc = cb08XzqhMapper.getXzqhmc(Integer.parseInt(kshz.getXzqdm()));
                kslsVo.setXzqmc(xzqhmc);
                LambdaQueryWrapper<Kslsju> queryWrapperKsls = new LambdaQueryWrapper<>();
                queryWrapperKsls.eq(Kslsju::getKqbh, kshz.getKqbh())
                        .eq(Kslsju::getDjflbh, kshz.getDjflbh());
                queryWrapperKsls.orderByDesc(Kslsju::getJdrq);
                List<Kslsju> kslsjus = kslsjuMapper.selectList(queryWrapperKsls);
                kslsVo.setKslsjus(kslsjus);
                kslsVoList.add(kslsVo);
            }
            resultPage.setRecords(kslsVoList);
            resultPage.setTotal(page.getTotal());
        }
        return resultPage;
    }

    /**
     * 添加/修改矿山历史数据
     * @param kslsju
     */
    @Override
    public void addAll(KslsVo kslsju) {

        LambdaQueryWrapper<Kslsju> queryWrapper = new LambdaQueryWrapper<>();
        //删除整个矿山历史
        if (kslsju.getKqbh()!=null&&kslsju.getDjflbh()!=null){
            queryWrapper.eq(Kslsju::getKqbh,kslsju.getKqbh()).eq(Kslsju::getDjflbh,kslsju.getDjflbh());
            kslsjuMapper.delete(queryWrapper);
        }
        List<Kslsju> kslsjus = kslsju.getKslsjus();
        if (kslsjus!=null&&kslsjus.size()>0){
            for (Kslsju kslsju1 : kslsjus){
                kslsju1.setKqbh(kslsju.getKqbh());
                kslsju1.setDjflbh(kslsju.getDjflbh());
            }
            this.saveBatch(kslsju.getKslsjus());
        }


    }
}
